const jwt = require('jsonwebtoken')
const whiteList = [
  '/',
  '/api/users/login',
  '/api/users/register',
  '/api/users/loginCode',
  '/api/get-test',
]
module.exports = () => {
  return async (ctx, next) => {
    if (whiteList.includes(ctx.path)) {
      await next()
    } else {
      const { token } = ctx.request.headers
      try {
        const userInfo = jwt.verify(token, ctx.app.config.secretKey)
        ctx.state.userInfo = userInfo
        await next()
      } catch (err) {
        ctx.body = { code: -1, msg: err }
      }
    }
  }
}
